package com.IntelligentCommunity.community.service.impl;

import com.IntelligentCommunity.common.core.domain.R;
import com.IntelligentCommunity.common.core.utils.StringUtils;
import com.IntelligentCommunity.common.security.utils.SecurityUtils;
import com.IntelligentCommunity.community.domain.*;
import com.IntelligentCommunity.community.mapper.StatisticsDataMapper;
import com.IntelligentCommunity.community.service.IRealDistrictService;
import com.IntelligentCommunity.community.service.IRealSecurityFacilitiesService;
import com.IntelligentCommunity.community.service.StatisticsDataService;
import com.IntelligentCommunity.system.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

/**
 * @Author：ZHP
 * @Package：com.IntelligentCommunity.community.service.impl
 * @Project：IntelligentCommunity
 * @name：StatisticsDataServiceImpl
 * @Date：2024/7/21 12:00
 * @Filename：StatisticsDataServiceImpl
 */
@Service
public class StatisticsDataServiceImpl implements StatisticsDataService {

    @Autowired
    private StatisticsDataMapper statisticsDataMapper;
    @Autowired
    private IRealDistrictService realDistrictService;
    @Autowired
    private IRealSecurityFacilitiesService realSecurityFacilitiesService;
    @Override
    public R getStatisticsData() {

        StatisticsData statisticsData = new StatisticsData();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser)){
            throw new RuntimeException("用户信息为空");
        }
        if (loginUser.getUserDistrictPermissions().isEmpty()){
            throw new RuntimeException("请你努力当牛马争取早日获取权限");
        }
        if (loginUser.getUserDistrictPermissions().contains("all")){
            Integer district = statisticsDataMapper.getDistrict(null);
            statisticsData.setDistrictNum(district);
            getData(statisticsData, null);
        }else {
            Set<String> userDistrictPermissions = loginUser.getUserDistrictPermissions();
            statisticsData.setDistrictNum(statisticsDataMapper.getDistrict(userDistrictPermissions));
            getData(statisticsData, userDistrictPermissions);
        }

        return R.ok(statisticsData);
    }

    @Override
    public DistrictCount getDistrictCount(Long id) {
        RealDistrict r = new RealDistrict();
        r.setId(id);
        List<RealDistrict> realDistricts = realDistrictService.selectRealDistrictList(r);
        RealDistrict realDistrict = realDistricts.get(0);
        Integer buildingCount = realDistrict.getBuildingCount(); //获取楼栋数量
        Integer populationNum = realDistrict.getPopulationNum();  //获取人口数量
        DistrictCount typeCount = realSecurityFacilitiesService.getTypeCount(id); //获取各类设施数量
        typeCount.setBuildingCount(buildingCount);
        typeCount.setPopulationNum(populationNum);
        return typeCount;
    }


    @Override
    public R<List<PopulationCount>> getPopulationCount() {
        List<PopulationCount> populationCount = statisticsDataMapper.getPopulationCount();
        return R.ok(populationCount);
    }

    @Override
    public R<List<EstateCount>> estateCount() {
        List<EstateCount> data = null;
        try {
            data = statisticsDataMapper.estateCount();
        }catch (Exception e){
            throw new RuntimeException("网络繁忙,正在重试");
        }
        return R.ok(data);
    }

    //获取数据
    private StatisticsData getData(StatisticsData statisticsData, Set<String> userDistrictPermissions){
        CompletableFuture<Void> securityFacilitiesFuture = CompletableFuture.runAsync(() -> {
            statisticsData.setSecurityFacilitiesNum(statisticsDataMapper.getSecurityFacilitiesNum(userDistrictPermissions));
        });

        CompletableFuture<Void> populationFuture = CompletableFuture.runAsync(() -> {
            statisticsData.setPopulationNum(statisticsDataMapper.getPopulation(userDistrictPermissions));
        });

        CompletableFuture<Void> realEstateNum = CompletableFuture.runAsync(() -> {
            statisticsData.setRealEstateNum(statisticsDataMapper.getRealEstateNum(userDistrictPermissions));
        });

        CompletableFuture<Void> vehicleNum = CompletableFuture.runAsync(() -> {
            statisticsData.setVehicleNum(statisticsDataMapper.getVehicleNum(userDistrictPermissions));
        });

        // 等待所有任务完成
        CompletableFuture.allOf(securityFacilitiesFuture, populationFuture, realEstateNum, vehicleNum).join();

        return statisticsData;

    }

}

